package com.data.modules.bpm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.data.modules.bpm.service.IFlowSerive;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
public class FlowSeriveImpl implements IFlowSerive {
    @Resource
    private TaskService taskService;
    
    @Resource
    private RuntimeService runtimeService;
    //业务表主键ID
    private String bizId = UUID.randomUUID().toString().toLowerCase();

    @Override
    public void startProcess(String bizKey) {
        if(StrUtil.isEmpty(bizKey)){
            bizKey = bizId;
        }
        Map<String, Object> maps = new HashMap<>();
        maps.put("day", 1);
        String processDefinitionKey = "act_leave";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, bizKey, maps);
        log.info("=============================>ProcessInstanceId=>{}", processInstance.getId());

        //申请完成
        Task task = taskService.createTaskQuery().processInstanceBusinessKey(bizKey).singleResult();
        log.info("当前任务：{}",task.toString());

        taskService.setAssignee(task.getId(), "zhangsan");
        taskService.complete(task.getId());

       Task nextTask =taskService.createTaskQuery().executionId(task.getExecutionId()).singleResult();
       log.info("下级任务：{}",nextTask.toString());
       log.info("流程实例Id={}",nextTask.getProcessInstanceId());
    }

    @Override
    public void apply(String bizKey) {
        if(StrUtil.isEmpty(bizKey)){
            bizKey = bizId;
        }
        //审批
        //bizId="9b7f0433-e262-4eb2-b086-e9159527988f";
        Task task = taskService.createTaskQuery().processInstanceBusinessKey(bizKey).singleResult();
        taskService.setAssignee(task.getId(), "lisi");
        taskService.complete(task.getId());
        log.info("流程实例Id={}",task.getProcessInstanceId());
    }

    @Override
    public void approvalSucess(String bizKey) {

    }

    @Override
    public void reject(String bizKey) {

    }
    
    
}
